package com.lixiang.userinfo.controller;

import java.util.HashMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.lixiang.userinfo.entity.Salesman;
import com.lixiang.userinfo.service.ISalesmanService;
import com.lixiang.userinfo.util.ApiResult;
import com.lixiang.userinfo.util.ResultCode;
/**
 * 销售信息控制器
 * @author 李湘
 * @date 2018-10-19
 */
//证明是controller层并且返回json
@RestController
public class SalesmanController {
	
	    //日志记录器
		private static Logger logger = LoggerFactory.getLogger(SalesmanController.class);
		
		@Autowired
		private ISalesmanService salesmanService;//自动注入依赖
		

	/**
	 * 分页查询销售信息
	 * @param page
	 * @param saleName
	 * @param saleSex
	 * @param saleState
	 * @return
	 */
	//http://localhost:8080/salesman/selectAll/1
	@RequestMapping(value="salesman/selectAll/{page}")
	@ResponseBody
	public ApiResult selectAll(@PathVariable("page")int page,String saleName,Integer saleSex,Integer saleState){
		try {
			//组织查询参数
			HashMap<String,Object> map=new HashMap<String,Object>();
			map.put("saleName", saleName);
			map.put("saleSex", saleSex);
			map.put("saleState", saleState);
			
			PageInfo<Salesman> pageInfo=salesmanService.selectAll(page,map);
			return ApiResult.success(pageInfo);
		} catch (Exception e) {
			logger.error("查询销售信息错误="+e.getMessage());
		}
		return ApiResult.failure(ResultCode.DATA_IS_WRONG);
	}
	
	/**
	 * 添加销售信息
	 * @param salesman
	 * @return
	 */
	@RequestMapping("salesman/save")
	public @ResponseBody ApiResult save(Salesman salesman){
		try {
			int rowCount=salesmanService.insertSelective(salesman);
			if(rowCount==1){
				return ApiResult.success();
			}
		} catch (Exception e) {
			logger.error("添加销售信息错误="+e.getMessage());
		}
		return ApiResult.failure(ResultCode.DATA_IS_WRONG);
	}
	
	/**
	 * 删除销售信息 将状态置为2 禁用
	 * @param roleId
	 * @return
	 */
	@RequestMapping("salesman/delete/{saleId}")
	@ResponseBody
	public ApiResult delete(@PathVariable("saleId")int saleId){
		try {
			int rowCount=salesmanService.delete(saleId);
			if(rowCount==1){
				return ApiResult.success();
			}
		} catch (Exception e) {
			logger.error("删除销售信息错误="+e.getMessage());
		}
		return ApiResult.failure(ResultCode.DATA_IS_WRONG);
	}
	
	/**
	 * 更新编辑销售信息
	 * @param salesman
	 * @return
	 */
	@RequestMapping("salesman/update")
	@ResponseBody
	public ApiResult update(Salesman salesman){
		try {
			int rowCount=salesmanService.updateByPrimaryKeySelective(salesman);
			if(rowCount==1){
				return ApiResult.success();
			}
		} catch (Exception e) {
			logger.error("更新销售信息错误="+e.getMessage());
		}
		return ApiResult.failure(ResultCode.DATA_IS_WRONG);
	}
	

}
